W12. Grammars and Computability
1. Theory
1.1 Thompson’s Construction: From Regular Expression to ε-NFSA
1.1.1 Motivation and Overview
Given a regular expression over an alphabet, we often need a computational device — a finite automaton — that accepts exactly the language the expression describes. Thompson’s Construction is a classical algorithm that converts any regular expression into an equivalent ε-Nondeterministic Finite State Automaton (ε-NFSA). The resulting automaton can then be used to match strings against the original regular expression (this is precisely how most regular-expression engines work internally).
The algorithm works recursively: a regular expression is broken into its constituent subexpressions, an automaton fragment is built for each, and the fragments are combined using a fixed set of composition rules. Every subexpression
1.1.2 Base Case Rules
There are two base cases that handle atomic regular expressions.
The empty expression
A single symbol
1.1.3 Composition Rules
Given automata
Concatenation
Union
Kleene star
1.1.4 Properties of the Construction
The ε-NFSA produced by Thompson’s Construction has a number of useful structural properties: it has at most
1.2 Kleene’s Algorithm: From FSA to Regular Expression
1.2.1 Overview
The converse problem — given a finite automaton, find a regular expression that describes its language — is solved by Kleene’s Algorithm (sometimes called the state elimination algorithm in variants). Given an FSA
Each such set is represented as a regular expression. The algorithm computes these expressions step by step for
Because no state has index greater than
1.2.2 Initial Expressions ( )
For
The
1.2.3 Recursive Step
Once
Intuition: a path from
This single recurrence is the heart of the algorithm; applying it for
1.3 Models for Languages: Operational vs. Generative
1.3.1 Two Fundamental Paradigms
Formal languages can be described in two fundamentally different ways:
- Operational models (automata) receive an input string and decide whether to accept or reject it. They are recognizers or transducers. Examples: FSA, PDA, Turing Machine.
- Generative models (grammars) provide a set of rewriting rules that can be applied to derive (generate) all and only the strings of a language. A grammar does not process input — it produces output.
Both paradigms describe the same underlying objects (formal languages), but from opposite directions. Each paradigm has advantages: automata are closer to implementation, while grammars are closer to specification.
1.3.2 Grammars in Parsing
In compiler construction, these two perspectives meet in parsing:
- A grammar (typically a context-free grammar or its BNF notation) defines the programming language syntax — it specifies what syntactically correct programs look like.
- An automaton (parser) processes source code — it reads the token stream and verifies that the program conforms to the grammar, recovering its syntactic structure for subsequent compilation phases.
Grammars may be nondeterministic in general, but actual parser generators (e.g., LL(1), LR(1)) often impose grammar restrictions and use limited lookahead to ensure deterministic parsing.
1.4 Chomsky Hierarchy
1.4.1 Classification of Grammars
Noam Chomsky (born 1928), the father of modern linguistics, introduced the formal classification of grammars in 1959. He observed that grammars differ in the form of their production rules, and that this form determines what class of languages the grammar can generate. His classification yields four nested types.
The four types form a strict hierarchy: every regular language is context-free, every context-free language is context-sensitive, and every context-sensitive language is recursively enumerable. The inclusions are proper — each class contains languages not in the class below it.
1.4.2 The Formal Definition of a Grammar
A grammar is a 4-tuple
is a finite set of nonterminal symbols (variables); is a finite set of terminal symbols (the actual alphabet of generated strings), disjoint from ; is a finite set of production rules (rewriting rules); is the start symbol.
A derivation is a sequence of strings
That is, the set of all terminal strings derivable from the start symbol.
1.5 Grammar Types in Depth
1.5.1 Type-0: Unrestricted Grammars
Type-0 (general or unrestricted) grammars impose no restrictions on production rules beyond the one that the left-hand side must be non-empty:
Both
Type-0 grammars correspond to Turing Machines — they generate exactly the recursively enumerable languages (those that a TM can accept, though it may loop forever on strings not in the language).
1.5.2 Type-1: Context-Sensitive Grammars
Type-1 (context-sensitive) grammars require all production rules to have the form:
where
The canonical example is
Why “linear bounded”? The usable tape length is a linear function
1.5.3 Type-2: Context-Free Grammars
Type-2 (context-free) grammars (CFGs) require all rules to have a single nonterminal on the left-hand side:
where
CFGs are of paramount practical importance because they are equivalent to Backus-Naur Form (BNF), the notation used to specify the syntax of virtually all programming languages. The connection was discovered in 1960: the ALGOL-60 language, defined using BNF by John Backus and Peter Naur, was formally identical to Chomsky’s context-free languages.
BNF writes rules as <LHS> ::= <RHS>, where <LHS> is a nonterminal and <RHS> is any sequence of terminals and nonterminals. For example, the BNF rule <expr> ::= <expr> + <term> | <term> defines expressions as either sums or single terms.
Context-free languages are recognised by Nondeterministic Pushdown Automata (NPDAs). The stack provides exactly the “one level of nesting” needed to match balanced structures like parentheses or
1.5.4 Type-3: Regular Grammars
Type-3 (regular) grammars impose the strictest constraints on production rules. All rules must be either right-linear or left-linear — but not a mix of both within the same grammar.
A right-linear grammar allows only rules of the form:
(a string of terminals followed by at most one nonterminal), or (a string of terminals only).
A left-linear grammar allows only rules of the form:
, or .
A grammar is regular if all its productions are right-linear, or all are left-linear; mixing the two orientations within one grammar is forbidden. Regular grammars generate exactly the regular languages — those accepted by Finite State Automata and described by regular expressions.
1.6 Correspondence Between Grammars and Automata
1.6.1 Regular Grammars and FSAs are Equivalent
The equivalence between Regular Grammars (RGs) and FSAs is constructive in both directions.
From FSA to RG. Given an FSA
(each state becomes a nonterminal); (the input alphabet becomes the terminals); (the initial state becomes the start symbol);- For each transition
, add rule ; - For each accepting state
, add rule .
The key invariant is
From RG to FSA. Given
(nonterminals become states; add one extra accepting state); ; ; ;- For each rule
, add transition ; - For each rule
(no trailing nonterminal), add transition .
This construction confirms that Regular Grammars, Finite State Automata, and Regular Expressions are three equivalent formalisms for describing the same family of languages.
1.6.2 Context-Free Grammars and NDPDAs are Equivalent
Context-free grammars are equivalent to Nondeterministic Pushdown Automata. The proof is the theoretical core of compiler construction. The intuition is as follows: an NPDA can simulate the left-most derivation of a CFG by storing the current sentential form on the stack. When the top of the stack is a nonterminal
Conversely, any NPDA can be converted to an equivalent CFG (this direction is more involved but equally constructive). Therefore:
1.6.3 Unrestricted Grammars and Turing Machines are Equivalent
General (unrestricted) grammars and Turing Machines recognise exactly the same class of languages — the recursively enumerable languages. Given a general grammar, a Turing Machine can simulate derivations by non-deterministically applying production rules. Conversely, given a Turing Machine, a general grammar can encode its configuration transitions as rewriting rules. This confirms the top of the Chomsky hierarchy.
1.6.4 Linear Bounded Automaton
A Linear Bounded Automaton (LBA) is a Turing Machine with one key restriction: the read/write head is constrained to remain within the portion of the tape occupied by the input string (bounded by end-markers [ and ]). The machine cannot access tape cells beyond the input boundaries.
The accessible tape length is a linear function of the input length
1.7 Post Correspondence Problem
1.7.1 Definition
The Post Correspondence Problem (PCP), introduced by Emil Post in 1946, is one of the simplest examples of an undecidable problem. It is often used in computability theory as a stepping-stone for proving other problems undecidable (by reduction from PCP).
Input: Two lists
Question: Does there exist a finite sequence of indices
That is, concatenating the
1.7.2 The Problem is Undecidable
No algorithm can decide the Post Correspondence Problem for arbitrary inputs. This means there is no Turing Machine that, given any PCP instance
1.8 Computability Theory
1.8.1 The Two Central Questions
The theory of computation addresses two fundamental questions:
- Mathematical: What can be computed? — Is there a mechanical procedure for solving this problem at all?
- Engineering: How efficiently can it be computed? — How much time or memory does an algorithm require?
Computability theory addresses the first question. It studies the limits of mechanical problem solving, identifying which problems are solvable by any computational device and which are not — regardless of how powerful the device is or how much time it is given.
A useful metaphor: computability theory studies the speed of light of computer science — the fundamental ceiling that no computation can exceed. Just as physical laws constrain what is physically possible, computability limits what is mathematically computable.
1.8.2 The Church-Turing Thesis
The Church-Turing Thesis is the central claim of computability theory. It states (informally):
Any function that is intuitively computable by a systematic mechanical procedure can be computed by a Turing Machine.
This is a thesis (not a theorem) because “intuitively computable” is not a mathematical concept. The thesis cannot be proved, only supported by evidence: every computational model ever proposed (lambda calculus, partial recursive functions, RAM machines, quantum computers, etc.) has been shown to be equivalent in expressive power to Turing Machines, or strictly weaker.
The practical consequence: if we wish to show that some problem cannot be solved by any algorithm, it suffices to show it cannot be solved by a Turing Machine.
1.8.3 The Halting Problem and Undecidability
The Halting Problem is the question: given a program
A problem is decidable (also called recursive or computable) if there exists a Turing Machine that:
- halts and accepts for every input in the language, and
- halts and rejects for every input not in the language.
A problem is semi-decidable (recursively enumerable) if a Turing Machine accepts every input in the language but may loop forever on inputs not in the language.
A problem is undecidable if no Turing Machine can decide it. Undecidable problems exist; the Halting Problem and the Post Correspondence Problem are classic examples.
2. Definitions
- Thompson’s Construction: An algorithm that converts any regular expression into an equivalent ε-NFSA by recursively building automaton fragments for each subexpression and composing them using fixed rules for concatenation, union, and Kleene star.
- ε-NFSA (ε-Nondeterministic Finite State Automaton): An NFSA extended with ε-transitions — transitions that consume no input symbol, allowing the automaton to change state spontaneously.
- Kleene’s Algorithm: An algorithm that converts a finite state automaton
into a regular expression by computing, for each state pair and each intermediate-state bound , the expression representing all paths from to through states of index at most . - Grammar: A 4-tuple
specifying a set of rewriting rules (productions) over nonterminal and terminal symbols that can derive strings of a language from the start symbol . - Derivation: A sequence of strings
where each step replaces a substring using one production rule. The derived language is the set of all terminal strings derivable from . - Chomsky Hierarchy: A classification of formal grammars (and the languages they generate) into four nested types — Type 0 (unrestricted), Type 1 (context-sensitive), Type 2 (context-free), and Type 3 (regular) — each corresponding to a class of automata.
- Unrestricted Grammar (Type-0): A grammar with no restrictions on productions
(other than ). Equivalent in power to Turing Machines; generates recursively enumerable languages. - Context-Sensitive Grammar (Type-1): A grammar whose rules have the form
( ). The nonterminal is rewritten in the context of its surrounding strings. Equivalent to Linear Bounded Automata. - Context-Free Grammar (Type-2): A grammar whose rules have the form
— a single nonterminal rewrites to any string, regardless of context. Equivalent to Nondeterministic Pushdown Automata. - Backus-Naur Form (BNF): A notation for context-free grammars used to specify programming language syntax, writing rules as
<nonterminal> ::= <RHS>. Formally equivalent to CFGs. - Regular Grammar (Type-3): A grammar whose rules are all right-linear (
or ) or all left-linear ( or ); mixing orientations is forbidden. Equivalent to FSAs and regular expressions. - Right-linear Grammar: A grammar where every rule has the form
or ; the nonterminal (if any) always appears at the right end of the right-hand side. - Left-linear Grammar: A grammar where every rule has the form
or ; the nonterminal (if any) always appears at the left end of the right-hand side. - Linear Bounded Automaton (LBA): A Turing Machine whose read/write head is restricted to the tape cells occupied by the input (bounded by end-markers). LBAs recognise exactly the context-sensitive languages.
- Parsing: The process of using an automaton (parser) to verify that an input string (program) conforms to a grammar (language specification) and to recover its syntactic structure.
- Post Correspondence Problem (PCP): The problem of determining, given two equal-length lists of strings
and , whether there exists a finite sequence of indices such that concatenating the -strings in that order equals concatenating the -strings in the same order. The PCP is undecidable. - Computability Theory: The branch of theoretical computer science that studies which problems can (and cannot) be solved by any mechanical computational procedure, regardless of time or memory.
- Decidable Problem: A problem for which there exists a Turing Machine that always halts and correctly answers “yes” or “no” for every input.
- Semi-decidable Problem: A problem for which a Turing Machine accepts every positive instance but may loop forever on negative instances. Also called recursively enumerable.
- Undecidable Problem: A problem for which no Turing Machine can decide it. No algorithm exists that correctly answers all instances.
- Church-Turing Thesis: The claim that every intuitively computable function can be computed by a Turing Machine — equivalently, that Turing Machines capture the full extent of mechanical computation.
3. Formulas
- Kleene’s Algorithm — initial step (
): - Kleene’s Algorithm — recursive step:
- Language accepted by FSA via Kleene:
- Thompson’s Construction — Union:
- Thompson’s Construction — Kleene Star:
4. Practice
4.1. Construct ε-NFSA for (Lab 11, Example 1)
Build the ε-NFSA for the regular expression
Click to see the solution
Step 1 — Build
Step 2 — Build
Step 3 — Build
Step 4 — Build
The resulting automaton has states
(the branch); (the branch).
Step 5 — Apply Kleene star. Introduce a new start state
(enter the union automaton); (loop back for another repetition); (bypass for zero repetitions); (accept after one or more repetitions).
Answer: The ε-NFSA has 9 states and recognises exactly
4.2. Apply Kleene’s Algorithm to a 2-State FSA (Lab 11, Example 2)
Find a regular expression for the language accepted by the FSA with states
Click to see the solution
Step
Inspect each pair of states:
: from to , direct transitions. , so the symbol is a self-loop. Since : . : from to , direct transitions. : . : from to , direct transitions. : . : from to , no self-loop transition. Since and no real symbol loops: .
Step
Apply the recurrence
Simplification:
, and . Any finite number of s is accepted, including zero.Going from
to (possibly looping) and then taking the -transition to .Taking the
-transition from to , then optionally looping in .Reaching
via , looping, then going back to via ; or staying in with zero moves.
Step
The only accepting state is
Substituting:
Simplification:
Since
Interpretation: the language consists of all strings of
4.3. Build ε-NFSA for (Lab 11, Task 1)
Using Thompson’s Construction, build an ε-NFSA for the regular expression
Click to see the solution
Step 1 — Build
Step 2 — Build
Step 3 — Build
(loop); (exit); (bypass for zero s).
Step 4 — Build
The automaton accepts exactly the strings
4.4. Build ε-NFSA for (Lab 11, Task 2)
Using Thompson’s Construction, build an ε-NFSA for the regular expression
Click to see the solution
Step 1 — Build
Step 2 — Build
Chain: the accepting state of
The full automaton: 1. Start at
The automaton accepts strings of length 3 of the form
4.5. Build ε-NFSA for (Lab 11, Task 3)
Using Thompson’s Construction, build an ε-NFSA for
Click to see the solution
Step 1 — Build
Step 2 — Build
Step 3 — Build
Step 4 — Build
The automaton accepts all strings starting with
4.6. Apply Kleene’s Algorithm to FSA 1 (Lab 11, Task 4)
Find a regular expression for the FSA with states
Click to see the solution
Step
(self-loop on , plus for staying). (direct -transition to ). (no transition from to ). (self-loop on ).
Step
Step
We need
Since
Interpretation: the language is all strings of the form (zero or more
4.7. Apply Kleene’s Algorithm to FSA 2 (Lab 11, Task 5)
Find a regular expression for the FSA with states
Click to see the solution
Step
Step
Step
Since
Interpretation: the automaton is in
4.8. Regular Expression for 3-State FSA (Homework 11, Task 1)
Find a regular expression that describes the language accepted by the FSA with states
Click to see the solution
Step
(both and go from to )
Step
Since
Step
Step
First compute
And
Since
Answer:
Interpretation: the only strings accepted are sequences of
4.9. Build ε-NFSA for (Homework 11, Task 2)
Using Thompson’s Construction, build an ε-NFSA for
Click to see the solution
Step 1 — Build
Step 2 — Build
Step 3 — Build
Introduce
(loop); (exit); (bypass).
Step 4 — Build
Step 5 — Concatenate
Merge
The automaton accepts strings consisting of an even number of